package com.ymk.cmd;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.base.service.WxService;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.user.UserInfoBO;
import com.ymk.core.code.cmd.UserCmdCode;
import com.ymk.core.constants.WxConstants;
import com.ymk.service.IUserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;


@Service
public class UserRealNameCmd extends ACmd {
    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        UserInfoBO reqData = super.getData(sign, UserInfoBO.class);
        String openId = request.getHeader(WxConstants.X_WX_OPENID);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("wxOpenId", openId)
                .setReturnStruct(sign.getReturnStruct());
        UserInfoBO respData = this.userService.get(paramsBuilder.build());
        if(Objects.isNull(respData)){
            throw new MallException("该用户不存在");
        }

        // 实名认证
        UserInfoBO userInfoBO = new UserInfoBO();
        JSONObject json = wxService.idCardOCR(reqData.getImgFront());
        if (!reqData.getRealName().equals(json.getString("name")) || !reqData.getIdCardNo().equals(json.getString("id"))){
            userInfoBO.setRealState(2);
            respData.setRealState(2);
        }else {
            userInfoBO.setRealState(1);
            respData.setRealState(1);
            respData.setRealName(reqData.getRealName());
            respData.setIdCardNo(reqData.getIdCardNo());
        }
        this.setModifiedData(sign,respData);
        userService.update(respData);

        respSign.setSource(super.getSource(userInfoBO));


        return respSign;
    }

    @Override
    public CmdSign executeSop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        UserInfoBO reqData = super.getData(sign, UserInfoBO.class);
        String openId = request.getHeader(WxConstants.X_WX_OPENID);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("wxOpenId", openId)
                .setReturnStruct(sign.getReturnStruct());
        UserInfoBO respData = this.userService.get(paramsBuilder.build());
        if(Objects.isNull(respData)){
            throw new MallException("该用户不存在");
        }
        // 实名认证
        UserInfoBO userInfoBO = new UserInfoBO();
        JSONObject json = wxService.idCardOCR(reqData.getImgFront());
        if (!reqData.getRealName().equals(json.getString("name")) || !reqData.getIdCardNo().equals(json.getString("id"))){
            userInfoBO.setRealState(2);
            respData.setRealState(2);
        }else {
            userInfoBO.setRealState(1);
            respData.setRealState(1);
            respData.setRealName(reqData.getRealName());
            respData.setIdCardNo(reqData.getIdCardNo());
        }
        this.setModifiedData(sign,respData);
        userService.update(respData);

        respSign.setSource(super.getSource(userInfoBO));


        return respSign;
    }



    @Override
    public String getCmdCode() {
        return UserCmdCode.USER_REAL_NAME;
    }

    @Override
    public CmdSign verify(HttpServletRequest request, CmdSign sign) throws Exception {
        UserInfoBO reqData = super.getData(sign, UserInfoBO.class);
        String openId = request.getHeader(WxConstants.X_WX_OPENID);
        if (StringUtils.isBlank(openId)) {
            throw new MallException("请求异常,header未发现openID");
        }
        if(StringUtils.isBlank(reqData.getRealName())){
            throw new MallException("真实姓名不能为空");
        }
        if(StringUtils.isBlank(reqData.getIdCardNo())){
            throw new MallException("身份证号码不能为空");
        }
        if(StringUtils.isBlank(reqData.getImgFront())){
            throw new MallException("身份证正面路径不能为空");
        }
        return null;
    }
    @Override
    public boolean anon(CmdSign sign) {
        return true;
    }

    @Autowired
    private WxService wxService;

    @Autowired
    private IUserInfoService userService;
}
